package com.ve.everything.searcher;


import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;

//import org.springframework.web.multipart.MultipartFile;

public class LuceneIndex {
    public static void indexDocument(
            File file
//            ,String id,
//            String owner,
//            String time,
//            String date
            , String[] tags
//            ,ArrayList<String> groupCodes,
//            String fileType,
//            String viewType,
//            String body
    ) throws Exception {
        IndexWriter indexWriter = LuceneInit.getIndexWriter();

        // new Document
        Document document = new Document();

        // add id
//        document.add(new StringField("id", id, Field.Store.YES));

        // add path
        document.add(new StringField("path", file.getAbsolutePath(), Field.Store.YES));

        // add file
        document.add(new StringField("file", file.getName(), Field.Store.YES));

        // add owner
//        document.add(new StringField("owner", owner, Field.Store.YES));

        // add timestamp

//        document.add(new StringField("createDate", time, Field.Store.YES));

        // add date
        document.add(new StringField("lastModified",String.valueOf(file.lastModified()) , Field.Store.YES));

        // add tags
        for (String tag : tags) {
            document.add(new StringField("tags", tag, Field.Store.YES));
        }

        // add groupCodes
//        for (String groupCode : groupCodes) {
//            document.add(new StringField("groupCodes", groupCode, Field.Store.YES));
//        }

        // add body
        document.add(new TextField("body", String.join("",Files.readAllLines(file.toPath())), Field.Store.YES));

        // add fileType
//        document.add(new StringField("fileType", fileType.split(";")[0], Field.Store.YES));

        // add viewType
//        document.add(new StringField("viewType", viewType, Field.Store.YES));

        // add the document and commit
        indexWriter.addDocument(document);
        indexWriter.commit();
    }
}
